package com.jeeplus.test.postgraduate.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.sys.service.OfficeService;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.mapper.AdmissionInfoMapper;
import com.jeeplus.test.jlnu.graduationInfo.domain.vo.NewGraduationInfoReqVo;
import com.jeeplus.test.jlnu.graduationInfo.mapper.NewGraduationInfoMapper;
import com.jeeplus.test.postgraduate.domain.vo.PostgraduateReqVo;
import com.jeeplus.test.postgraduate.mapper.PostgraduateMapper;
import com.jeeplus.test.postgraduate.service.PostgraduateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PostgraduateServiceImpl implements PostgraduateService {


    @Autowired
    private PostgraduateMapper postgraduateMapper;


    @Autowired
    private OfficeService officeService;

    @Autowired
    private AdmissionInfoMapper admissionInfoMapper;

    @Autowired
    private NewGraduationInfoMapper graduationInfoMapper;

    @Override
    public Page<Map<String, Object>> getAdmissionInfoPage(PostgraduateReqVo admissionInfoReqVo, Page<PostgraduateReqVo> page) {
        if (StringUtils.isNotEmpty(admissionInfoReqVo.getOfficeId())) {
            //获取机构下所有层级
            List<String> officesList = officeService.getOfficesTreeByParentId(admissionInfoReqVo.getOfficeId());
            officesList.add(admissionInfoReqVo.getOfficeId());
            admissionInfoReqVo.setOfficesList(officesList);
        }

        // 处理type
        if (StringUtils.isNotEmpty(admissionInfoReqVo.getApproveState())) {
            String[] arr = admissionInfoReqVo.getApproveState().split(",");
            List<String> list = Arrays.asList(arr);
            admissionInfoReqVo.setApproveStateList(list);
        }


        // 1. 获取分页结果和字段配置信息
        Page<PostgraduateReqVo> admissionInfoPage = postgraduateMapper.getAdmissionInfoPage(admissionInfoReqVo, page);

        List<PostgraduateReqVo> records = admissionInfoPage.getRecords();

        List<Map<String, Object>> modifiedRecords = new ArrayList<>();

        // 2. 遍历每条记录
        for (PostgraduateReqVo record : records) {
            Map<String, Object> recordMap = new HashMap<>();

            // 3. 将 AdmissionInfoRespVo 中的字段放入 map
            Field[] fields = PostgraduateReqVo.class.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    // 获取字段值并添加到 map 中
                    Object value = field.get(record);
                    recordMap.put(field.getName(), value);
                } catch (IllegalAccessException e) {
                    //e.printStackTrace();
                }
            }

            // 5. 添加处理后的记录
            modifiedRecords.add(recordMap);
        }

        // 6. 将处理后的记录封装到新的分页对象中返回
        Page<Map<String, Object>> modifiedPage = new Page<>(admissionInfoPage.getCurrent(), admissionInfoPage.getSize(), admissionInfoPage.getTotal());
        modifiedPage.setRecords(modifiedRecords);

        return modifiedPage;

    }


    @Override
    public void approval(List<AdmissionInfo> admissionInfoRespVos) {
        //批量更新
        for (int i = 0; i < admissionInfoRespVos.size(); i++) {

            //审批通过后数据同步到毕业信息中
               if(admissionInfoRespVos.get(i).getApproveState().equals(3)){

                addGraduation(admissionInfoRespVos.get(i).getStudentId());
            }
            postgraduateMapper.updateAdmissionsById(admissionInfoRespVos.get(i));
        }
    }

    @Override
    public void update(PostgraduateReqVo admissionInfoReqVo) {

        postgraduateMapper.updateJlnuPostgraduate(admissionInfoReqVo);

        //修改主表

        AdmissionInfo admissionInfo = new AdmissionInfo();

        admissionInfo.setStudentId(admissionInfoReqVo.getZxshxh());

        //admissionInfo.setExamineeNum(StudentInfoEntity.getKsh());//考生号无
        //admissionInfo.setCategory(registrarOffice.getKlmc());科类无
        admissionInfo.setAdmissionNum(admissionInfoReqVo.getZkzh());//准考证号
        admissionInfo.setName(admissionInfoReqVo.getXm());//姓名
        admissionInfo.setGender(admissionInfoReqVo.getXb());//性别

        // 将字符串转换为 Date 对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthDate = sdf.parse(admissionInfoReqVo.getCsrq());
            //System.out.println("出生日期（Date 类型）: " + birthDate);
            admissionInfo.setBirthday(birthDate);//出生日期
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        admissionInfo.setPoliticalOutlook(admissionInfoReqVo.getZzmm());//政治面貌
        admissionInfo.setNation(admissionInfoReqVo.getMz());//民族
        admissionInfo.setIdCard(admissionInfoReqVo.getSfzh());//身份证号
        //admissionInfo.setPlanNature(StudentInfoEntity.get());//计划性质
        // 设置录取学校为吉林师范大学
        //admissionInfo.setAdmissionSchools(admissionInfoReqVo.getAdmissionSchools());//录取学校
        admissionInfo.setAdmissionAcademy(admissionInfoReqVo.getXy());//录取学院
        admissionInfo.setAdmissionMajor(admissionInfoReqVo.getZymc());//录取专业
        admissionInfo.setAdmissionTime(admissionInfoReqVo.getNd());//录取时间

        admissionInfo.setEducationalSystem(admissionInfoReqVo.getXz());//学制

        admissionInfo.setExamineeSort(admissionInfoReqVo.getXlbl());//考生类别
        admissionInfo.setHomeAddress(admissionInfoReqVo.getJgszd());//家庭（邮寄）地址

        //admissionInfo.setInfoSource("教务处招生数据");

        admissionInfoMapper.updateAdmissions(admissionInfo);


    }

    //毕业信息数据同步
    public void addGraduation(String studentId){

        try {
            NewGraduationInfoReqVo graduationInfoReqVo = new NewGraduationInfoReqVo();
            AdmissionInfo admissionInfo =  admissionInfoMapper.selectBatchId(studentId);
            BeanUtils.copyProperties(admissionInfo, graduationInfoReqVo);
            graduationInfoReqVo.setEnrollmentDate(admissionInfo.getAdmissionTime());
            graduationInfoReqVo.setInformationSource(admissionInfo.getInfoSource());
            graduationInfoReqVo.setReceiver(admissionInfo.getRemarks());
            graduationInfoMapper.addGraduationInfo(graduationInfoReqVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
